package com.sunshineedu.sunshineeducationspring.controller;

import com.sunshineedu.sunshineeducationspring.entity.ReturnResult;
import com.sunshineedu.sunshineeducationspring.entity.User;
import com.sunshineedu.sunshineeducationspring.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/User")
@CrossOrigin("*")
public class UserController {

    Thread thread;

    ReturnResult returnResult;

    @Resource
    UserService userService;
    @PostMapping("/Login")
    public ReturnResult login(@RequestBody User data) {
        String wxId = data.getWxId();
        String password = data.getPassword();
        User user = userService.findAllByWxId(wxId);
        if(user.getPassword() != null && password.equals(user.getPassword())){
            returnResult=new ReturnResult("1","登录成功",user.exceptPassword());
        }else{
            returnResult=new ReturnResult("-1","登录失败，请检查用户名或者密码",null);
        }
        return returnResult;
    }
    @PostMapping("/Register")
    public ReturnResult register(@RequestBody User data) {
        if(data.getUsername() == null || data.getWxId() == null){
            return new ReturnResult("-1","微信号或用户名未填写",null);
        }else if(userService.findAllByUsername(data.getUsername()) != null){
            return new ReturnResult("-1","用户名已存在",null);
        }else if(userService.findAllByWxId(data.getWxId()) != null){
            return new ReturnResult("-1","微信号已存在",null);
        }else if(userService.save(data) == null){
            return new ReturnResult("-1","注册失败",null);
        }else{
            return new ReturnResult("1","注册成功",null);
        }
    }
    @PostMapping("/Logout")
    public ReturnResult logout() {
        returnResult=new ReturnResult("-1","登出",null);
        return returnResult;
    }
    @PostMapping("/UpdatePassword")
    public ReturnResult updatePassword() {
        returnResult=new ReturnResult("-1","修改密码",null);
        return returnResult;
    }

}
